package com.my.study.zookeeper_study;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.concurrent.CountDownLatch;

/**
 * @author zlx
 * @Description 创建Zookeeper会话
 * @Date 2020-08-19
 * @Param
 * @return
 **/
public class MyZookeeperStudy implements Watcher {


    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    public static void main(String[] args) {

        try {
            String connectStr = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
            int sessionTimeOut = 5000;
            ZooKeeper zooKeeper = new ZooKeeper(connectStr, sessionTimeOut, new MyZookeeperStudy());
            System.out.println(zooKeeper.getState());
            countDownLatch.await();

        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("zookeeper--------");
    }

    @Override
    public void process(WatchedEvent event) {

        System.out.println(event.getState());
        if(event.getState() == Event.KeeperState.SyncConnected){
            System.out.println("haahahha=========");
            countDownLatch.countDown();
        }
    }
}
